class Solution {
    public int minDays(int[] bloomDay, int m, int k) {
        if (m * k > bloomDay.length) {
            return -1;
        }
        int left = 0;
        int right = 0;
        for (int i = 0; i < bloomDay.length; i++) {
            right = Math.max(right, bloomDay[i]);
        }
        while (left < right) {
            int mid = (left + right) >> 1;
            if (isValid(bloomDay, m, k, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return right;
    }
    private boolean isValid(int[] bloomDay, int m, int k, int day) {
        // day 天时，能制作多少束花
        // 要求k朵花相邻
        int count = 0;
        int bunthCount = 0;
        for (int i = 0; i < bloomDay.length; i++) {
            if (bloomDay[i] <= day) {
                count++;
                if (count == k) {
                    bunthCount++;
                    count = 0;
                }
            } else {
                count = 0;
            }
        }
        return bunthCount >= m;
    }
}
